package com.sunni.resolver;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
        // 利用modelAndView，跳转到指定页面。
        ModelAndView modelAndView = new ModelAndView();

        // 处理每种异常
        if (e instanceof ClassCastException){
            modelAndView.addObject("info","类型转换异常");
        }else if (e instanceof ArithmeticException){
            modelAndView.addObject("info","1/0的异常");
        }else {
            modelAndView.addObject("info","未知异常");
        }

        modelAndView.setViewName("error_bymyself");

        return modelAndView;
    }
}
